using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel.Design;
using EnvDTE;
using System.Globalization;
using Microsoft.Xna.Framework;

namespace XnaDesignerGenerator
{
    public class GameComponentTypeListConverter : TypeListConverter
    {
        // Methods
        public GameComponentTypeListConverter()
            : base(typeof(DrawableGameComponent).GetInterfaces())
        {
        }

        private void AnalyseCodeElement(CodeElement ce, string ns, IList classes)
        {
            if (ce is CodeNamespace)
            {
                ns = ns + ce.FullName + ".";
            }
            if ((ce is CodeClass) && !(!typeof(GameComponent).IsAssignableFrom(Type.GetType(ns + ce.Name)) || Type.GetType(ns + ce.Name).IsAbstract))
            {
                classes.Add(Type.GetType(ns + ce.Name));
            }
            if ((ce is CodeNamespace) && (((CodeNamespace)ce).Members != null))
            {
                foreach (CodeElement element in ((CodeNamespace)ce).Members)
                {
                    this.AnalyseCodeElement(element, ns, classes);
                }
            }
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                return Type.GetType(value.ToString());
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            IList classes = new ArrayList();
            XnaGameComponent instance = (XnaGameComponent)context.Instance;
            TypeConverter.StandardValuesCollection standardValues = base.GetStandardValues(context);
            if (instance.Assembly != null)
            {
                foreach (Module module in instance.Assembly.GetModules())
                {
                    foreach (Type type in module.GetTypes())
                    {
                        if (type.GetInterface(typeof(IGameComponent).FullName) != null)
                        {
                            classes.Add(type);
                        }
                    }
                }
            }
            if (instance.Project != null)
            {
                try
                {
                    foreach (object obj2 in instance.Project.ProjectItems)
                    {
                        ProjectItem item = (ProjectItem)obj2;
                        if ((item.FileCodeModel != null) && (item.FileCodeModel.CodeElements != null))
                        {
                            foreach (CodeElement element in item.FileCodeModel.CodeElements)
                            {
                                this.AnalyseCodeElement(element, "", classes);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message + "\n" + exception.StackTrace, exception.GetType().FullName);
                }
            }
            return new TypeConverter.StandardValuesCollection(classes);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }
    }
}
